package comet

import (
	"io"
	"sync"

	"github.com/tinylib/msgp/msgp"
)

type ExitStatus int

const (
	NoneExit ExitStatus = iota
	ClientExit
	ServerExit
)

type MT uint8

const (
	P_OPEN MT = iota
	P_REOPEN
	P_CLOSE
	P_PING
	P_PONG
	P_APPLICATION
	P_AUTH
	P_PROTOCOL
	P_ERROR
	P_AUTHERRROR
)

type DN uint8

const (
	SYSTEM_NO DN = iota
)

type DE uint8

type App interface {
	GetId() string
	GetVersion() string
	GetToken() string
}
type Packet interface {
	ReadBytes() []byte
	Reader() io.Reader
}

//go:generate msgp
//msgp:tuple AppPacket
type AppPacket struct {
	Id      string `msg:"id"`
	Version string `msg:"version"`
	Token   string `msg:"token"`
}

func NewAppData() *AppPacket {
	return &AppPacket{}
}

//封包
func (p *AppPacket) Encode(writer io.Writer) error {
	return msgp.Encode(writer, p)
}

//解包
func (p *AppPacket) Decode(reader io.Reader) error {
	return msgp.Decode(reader, p)
}

func (p *AppPacket) GetId() string {
	return p.Id
}

func (p *AppPacket) GetVersion() string {
	return p.Version
}

func (p *AppPacket) GetToken() string {
	return p.Token
}

var (
	appPool = sync.Pool{}
)

func init() {
	appPool.New = func() interface{} {
		return NewAppData()
	}
}

func GetApp() *AppPacket {
	p := appPool.Get().(*AppPacket)
	return p
}

func (p *AppPacket) Release() {
	if p != nil {
		appPool.Put(p)
	}
}
